<template>
  <div class="score-case">
    <div class="table">
      <table>
        <thead>
          <tr>
            <th>编号</th>
            <th>科目</th>
            <th>成绩</th>
            <th>考试时间</th>
            <th>操作</th>
          </tr>
        </thead>
        <tbody>
          <!-- 渲染 -->
          <tr v-for="item in list" :key="item.id" >
            <td>{{item.id}}</td>
            <td>{{item.subject}}</td>
            <!-- 分数低于60变红色 用动态属性 {样式：布尔值} -->
            <td :class="{red: item.score<60 }">{{item. score}}</td>
            <td>{{ formatDate(item.date)}}</td>
            <!-- 添加点击事件，根据id删除 a标签 -->
            <td><a href="#" @click.prevent="delFn(item.id)">删除</a></td>
          </tr>
         
        </tbody>
        <tbody >
          <!-- 数组长度为0时显示 -->
          <tr v-show="list.length===0" >
            <td colspan="5">
              <span class="none">暂无数据</span>
            </td>
          </tr>
        </tbody>
        <tfoot  >
            <!-- 数组长度小于0时隐藏 -->
          <tr v-show="this.list.length>0" >
            <td colspan="5">
              <span>总分：{{SumScore}}</span>
              <span style="margin-left:50px">平均分：{{AveScore}}</span>
            </td>
          </tr>
        </tfoot>
      </table>
    </div>
    <div class="form">
      <div class="form-item">
        <div class="label">科目：</div>
        <div class="input">
          <input type="text" placeholder="请输入科目" v-model.trim="subject" />
          <!-- 双向绑定  去除首位空格-->
        </div>
      </div>
      <div class="form-item">
        <div class="label">分数：</div>
        <div class="input">
          <input type="text" placeholder="请输入分数" v-model.number="score" />
          <!-- 数字类型 -->
        </div>
      </div>
      <div class="form-item">
        <div class="label"></div>
        <div class="input">
          <button class="submit"  @click="addFn">添加</button>
          <!-- 绑定点击事件 -->
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import moment from 'moment'
export default {
  name: 'ScoreCase',
  data () {
    return {
       list:JSON.parse(localStorage.getItem('scoreCase')) || [],
      subject: '',
      score: ''
    }
  },
  methods:{
    delFn(id){
  // console.log(id);
  //从数组中删除 位置 ，个数
  // this.list.splice(this.id ,1)  
  this.list=this.list.filter(item=>item.id!==id)
    },
    //非空检验 分数合理 添加一个对象 和对象结构一样
    addFn(){
      if(this.subject!=='' && this.score>=0 &&this.score<=100){
        this.list.push({
          //判断是否有长度 ，有的话最后一个id+1 不然就是1
        id:this.list.length>0 ? this.list.at(-1).id+1 :1,
        subject: this.subject,
        score:this.score,
        data:new Date()
      
      })
      }else{
        alert('输入正确格式')
      }
     
      this.subject='',
       this.score=''
    },
    	// 格式化时间
    formatDate(input) {
        return moment(input).format('YYYY-MM-DD HH:mm:ss')
    }
  },
  //计算属性 多个影响一个 
  computed:{
    //总分 累加
      SumScore(){
        return this.list.reduce((sum,per)=>sum+per.score,0)
      },
      //平均分
      AveScore(){
        //先判断有没有长度
        return this.list.length ?(this.SumScore/this.list.length).toFixed(2):0
      }

  },
  //实现本地存储
  watch:{
     list:{
       deep:true,
       handler(newList){
          localStorage.setItem('scoreCase',JSON.stringify(newList))
       }
     }
      
  }
};
</script>

<style lang="less">
.score-case {
  width: 1000px;
  margin: 50px auto;
  display: flex;
  .table {
    flex: 4;
    table {
      width: 100%;
      border-spacing: 0;
      border-top: 1px solid #ccc;
      border-left: 1px solid #ccc;
      th {
        background: #f5f5f5;
      }
      tr:hover td {
        background: #f5f5f5;
      }
      td,
      th {
        border-bottom: 1px solid #ccc;
        border-right: 1px solid #ccc;
        text-align: center;
        padding: 10px;
        &.red {
          color: red;
        }
      }
    }
    .none {
      height: 100px;
      line-height: 100px;
      color: #999;
    }
  }
  .form {
    flex: 1;
    padding: 20px;
    .form-item {
      display: flex;
      margin-bottom: 20px;
      align-items: center;
    }
    .form-item .label {
      width: 60px;
      text-align: right;
      font-size: 14px;
    }
    .form-item .input {
      flex: 1;
    }
    .form-item input,
    .form-item select {
      appearance: none;
      outline: none;
      border: 1px solid #ccc;
      width: 200px;
      height: 40px;
      box-sizing: border-box;
      padding: 10px;
      color: #666;
    }
    .form-item input::placeholder {
      color: #666;
    }
    .form-item .cancel,
    .form-item .submit {
      appearance: none;
      outline: none;
      border: 1px solid #ccc;
      border-radius: 4px;
      padding: 4px 10px;
      margin-right: 10px;
      font-size: 12px;
      background: #ccc;
    }
    .form-item .submit {
      border-color: #069;
      background: #069;
      color: #fff;
    }
  }
}
</style>